package com.qidianit.mes.pro.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qidianit.mes.pojo.Result;
import com.qidianit.mes.pojo.pro.ProductionOrder;
import com.qidianit.mes.pojo.pro.WorkflowProduct;
import com.qidianit.mes.pro.mapper.ProductionOrderMapper;
import com.qidianit.mes.pro.service.ProductionOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.List;

/**
 * @Description:
 * @Author: YanJiRan
 * @Date: 2025/9/20 - 下午2:18
 */
@Service
public class ProductionOrderServiceImpl implements ProductionOrderService {
    @Autowired
    private ProductionOrderMapper productionOrderMapper;
    @Override
    public Result<PageInfo<ProductionOrder>> selectList(ProductionOrder productionOrder, int pageNum, int pageSize) {
        //开启分页
        PageHelper.startPage(pageNum,pageSize);
        List<ProductionOrder> productionOrders = productionOrderMapper.selectList(productionOrder);
        return Result.success(new PageInfo<>(productionOrders));
    }

    @Override
    public Result<ProductionOrder> selectById(Long id) {
        return Result.success(productionOrderMapper.selectById(id));
    }

    @Override
    public Result<ProductionOrder> saveProductionOrder(ProductionOrder productionOrder) {
        /*// 新增时默认设置状态为1（待审核）
        if (productionOrder.getId() == null && productionOrder.getStatus() == null) {
            productionOrder.setStatus(1);
        }*/
        //查重，查询工单编号orderCode,新建个对象存放当前新放的orderCode
        ProductionOrder newProductionOrder = new ProductionOrder();
        newProductionOrder.setProductionOrderCode(productionOrder.getProductionOrderCode());
        //查询数据表中是否有新放的orderCode，有就放进集合中
        List<ProductionOrder> productionOrder1 = productionOrderMapper.selectList(newProductionOrder);

        if(productionOrder.getId() == null){
            if(!productionOrder1.isEmpty()){
                return Result.error("工单编号重复");
            }
        }

        return productionOrderMapper.saveProductionOrder(productionOrder) == 1 ? Result.success(productionOrder) : Result.error("新增工单失败");
    }

    @Override
    public Result<ProductionOrder> updateById(ProductionOrder productionOrder) {
        // 查重，查询工单编号orderCode和id,新建个对象存放当前新放的orderCode和id
        ProductionOrder newProductionOrder = new ProductionOrder();
        newProductionOrder.setProductionOrderCode(productionOrder.getProductionOrderCode());
        newProductionOrder.setId(productionOrder.getId());
        // 查询数据表中是否有新放的orderCode，有就放进集合中
        List<ProductionOrder> productionOrder1 = productionOrderMapper.selectList(newProductionOrder);
        // 编辑操作
        if(!productionOrder1.isEmpty()){
            // 检查是否除了当前编辑的记录外还有其他重复的记录
            for(ProductionOrder order : productionOrder1){
                if(!order.getId().equals(productionOrder.getId())){
                    return Result.error("工单编号重复");
                }
            }
        }
        return productionOrderMapper.updateById(productionOrder) == 1 ? Result.success(productionOrder) : Result.error("编辑工单失败");
    }

    @Override
    public Result<ProductionOrder> deleteById(@PathVariable("id") Long id) {
        return productionOrderMapper.deleteById(id) == 1 ? Result.success() : Result.error("操作失败");
    }
}
